﻿<#
.Synopsis
   将某个文件通过FTP上传到服务器上
.DESCRIPTION
   将某个文件通过FTP上传到服务器上[详细描述]
.EXAMPLE
   如何使用此 cmdlet 的示例
.EXAMPLE
   另一个如何使用此 cmdlet 的示例
#>
function Ftp_UploadFile
{
    [CmdletBinding()]
    #[OutputType([int])]
    Param
        (
        [Parameter(Mandatory = $true,Position=0 )]
        [string]$LocalFile = "localFileFullPath",     
        [Parameter(Mandatory = $true,Position=1 )]
        [string]$RemoteFile = "desFileFullPath",     
        [Parameter(Mandatory = $true,Position=2 )]
        [string]$Username = "Username",     
        [Parameter(Mandatory = $true,Position=3 )]  
        [string]$Password = "Password"    
    )

    Begin
    {
    }
    Process
    {
       # Create FTP Rquest Object
        $FTPRequest = [System.Net.FtpWebRequest]::Create("$RemoteFile")
        $FTPRequest = [System.Net.FtpWebRequest]$FTPRequest
        $FTPRequest.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile
        $FTPRequest.Credentials = new-object System.Net.NetworkCredential($Username, $Password)
        $FTPRequest.UseBinary = $true
        $FTPRequest.UsePassive = $true
        # Read the File for Upload
        $FileContent = gc -en byte $LocalFile
        $FTPRequest.ContentLength = $FileContent.Length
        # Get Stream Request by bytes
        $Run = $FTPRequest.GetRequestStream()
        $Run.Write($FileContent, 0, $FileContent.Length)
        #return upload status ---By Self
        $response.StatusDescription
        # Cleanup
        $Run.Close()
        $Run.Dispose()
    }
    End
    {
    }
}

<#
.Synopsis
   将某个文件通过FTP下载到本地
.DESCRIPTION
   将某个文件通过FTP下载到本地[详细描述]
.EXAMPLE
   如何使用此 cmdlet 的示例
.EXAMPLE
   另一个如何使用此 cmdlet 的示例
#>
function Ftp_DownloadFile
{
    [CmdletBinding()]
    [OutputType([int])]
    Param
    (
        [Parameter(Mandatory = $true,Position=0 )]
        [string]$RemoteFile = "desFileFullPath",     
        [Parameter(Mandatory = $true,Position=1 )]
        [string]$LocalFile = "localFileFullPath",     
        [Parameter(Mandatory = $true,Position=2 )]
        [string]$Username = "Username",     
        [Parameter(Mandatory = $true,Position=3 )]  
        [string]$Password = "Password"    
    )

    Begin
    {
    }
    Process
    {
        # Create a FTPWebRequest 
        $FTPRequest = [System.Net.FtpWebRequest]::Create($RemoteFile) 
        $FTPRequest.Credentials = New-Object System.Net.NetworkCredential($Username,$Password) 
        $FTPRequest.Method = [System.Net.WebRequestMethods+Ftp]::DownloadFile 
        $FTPRequest.UseBinary = $true 
        $FTPRequest.KeepAlive = $false
        # Send the ftp request
        $FTPResponse = $FTPRequest.GetResponse() 
        # Get a download stream from the server response 
        $ResponseStream = $FTPResponse.GetResponseStream() 
        # Create the target file on the local system and the download buffer 
        $LocalFileFile = New-Object IO.FileStream ($LocalFile,[IO.FileMode]::Create) 
        [byte[]]$ReadBuffer = New-Object byte[] 1024 
        # Loop through the download 
        do { 
            $ReadLength = $ResponseStream.Read($ReadBuffer,0,1024) 
            $LocalFileFile.Write($ReadBuffer,0,$ReadLength) 
        } 
        while ($ReadLength -ne 0)
        #return upload status ---By Self
        $response.StatusDescription
    }
    End
    {
    }
}

<#
.Synopsis
   简短描述
.DESCRIPTION
   详细描述
.EXAMPLE
   如何使用此 cmdlet 的示例
.EXAMPLE
   另一个如何使用此 cmdlet 的示例
#>
function Ftp_UploadFold
{
    [CmdletBinding()]
    [OutputType([int])]
    Param
    (
        <#      Mandatory --->>> 强制的;命令的;受委托的    #>
        [Parameter(Mandatory = $true,Position=0 )]
        [string]$LocalFold = "localFileFullPath",     
        [Parameter(Mandatory = $true,Position=1 )]
        [string]$RemoteFold = "desFileFullPath",
        [Parameter(Mandatory = $false,Position=2 )]
        [string]$fileMatchStandardInLocalFold = "",      
        [Parameter(Mandatory = $true,Position=3 )]
        [string]$Username = "Username",     
        [Parameter(Mandatory = $true,Position=4 )]  
        [string]$Password = "Password"   
    )

    Begin
    {
    }
    Process
    {
        #we specify the directory where all files that we want to upload  
        $Dir=$LocalFold  
 
        #ftp server 
        $ftp = $RemoteFold
        $user = $Username 
        $pass = $Password  
 
        $webclient = New-Object System.Net.WebClient 
 
        $webclient.Credentials = New-Object System.Net.NetworkCredential($user,$pass)  
 
        #list every sql server trace file 
        foreach($item in (dir $Dir  $fileMatchStandardInLocalFold)){  #"*.trc"
        "Uploading $item..." 
        $uri = New-Object System.Uri($ftp+$item.Name) 
        $webclient.UploadFile($uri, $item.FullName) 
        } 
    }
    End
    {
    }
}
#--------------------------------------------------------Test---------------------------------------------------------------------
#Ftp_UploadFile "D:\bbb\OrignData.txt"  "ftp://192.168.120.211/org.txt" "ftp041" "ftp@041"
#Ftp_DownloadFile  "ftp://192.168.120.211/fileTransferStatistics2014-09-22.docx" "D:\bbb\OrignData.docx"  "ftp041" "ftp@041"
#Ftp_UploadFold "D:\bbb\Check"  "ftp://192.168.120.211/Check/" "" "ftp041" "ftp@041"